﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

namespace Tesla.Util.UIUtilities
{
    [ToolboxData("<{0}:TeslaDataGrid runat=server></{0}:TeslaDataGrid>")]
    public class TeslaDataGrid : GridView
    {
        //Key values for all the configurable grid styles.  
        private const string _GRID_HEADER_BACK_COLOR_KEY = "GridHeaderBackColor";
        private const string _GRID_HEADER_FORE_COLOR_KEY = "GridHeaderForeColor";
        private const string _GRID_HEADER_BORDER_COLOR_KEY = "GridHeaderBorderColor";
        private const string _GRID_ITEM_BACK_COLOR_KEY = "GridItemBackColor";
        private const string _GRID_ITEM_FORE_COLOR_KEY = "GridItemForeColor";
        private const string _GRID_ITEM_BORDER_COLOR_KEY = "GridItemBorderColor";
        private const string _GRID_ALTR_ITEM_BACK_COLOR_KEY = "GridAltrItemBackColor";
        private const string _GRID_ALTR_ITEM_FORE_COLOR_KEY = "GridAltrItemForeColor";
        private const string _GRID_ALTR_ITEM_BORDER_COLOR_KEY = "GridAltrItemBorderColor";
        private const string _GRID_PAGER_BACK_COLOR_KEY = "GridPagerBackColor";

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        // <summary>	
        // Overrides default method to configure the Grid styles
      //   </summary>

        protected override void OnLoad(EventArgs e)
        {
            SetUIProperties();
            base.OnLoad(e);

        }
        /// <summary>	
        /// Sets styles for all the Grid elements like items, header, alternate intems and pager.
        /// Reads actual style information from the configuration file.
        /// </summary>

        
        private void SetUIProperties()
        {
            AutoGenerateColumns = false;
            GridLines = GridLines.Both;
            BorderColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_ITEM_BORDER_COLOR_KEY));

            this.Attributes.Add("bordercolor", ControlUtilities.GetConfigInformation(_GRID_ITEM_BORDER_COLOR_KEY));
            //Setting the custom GridView item style 
            RowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_ITEM_BACK_COLOR_KEY));
            RowStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_ITEM_FORE_COLOR_KEY));
            RowStyle.BorderStyle = BorderStyle.Solid;
            RowStyle.BorderColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_ITEM_BORDER_COLOR_KEY));
            RowStyle.BorderWidth = new Unit("1");

            //Setting the custom GridView header style 
            HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_HEADER_BACK_COLOR_KEY));
            HeaderStyle.BorderStyle = BorderStyle.Solid;
            HeaderStyle.BorderColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_HEADER_BORDER_COLOR_KEY));
            HeaderStyle.BorderWidth = new Unit("1");

            //Setting the custom GridView alternate style 
            AlternatingRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_ALTR_ITEM_BACK_COLOR_KEY));
            AlternatingRowStyle.BorderStyle = BorderStyle.Solid;
            AlternatingRowStyle.BorderColor = System.Drawing.ColorTranslator.FromHtml(ControlUtilities.GetConfigInformation(_GRID_ALTR_ITEM_BORDER_COLOR_KEY));
            AlternatingRowStyle.BorderWidth = new Unit("1");
        }
    }
}
